package com.debao;

import com.alibaba.fastjson2.JSONObject;
import com.debao.common.annotation.Anonymous;
import com.debao.common.constant.WechatConstants;
import com.debao.common.core.controller.BaseController;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.core.domain.entity.SysUser;
import com.debao.common.core.page.TableDataInfo;
import com.debao.common.enums.OrderStatusEnum;
import com.debao.common.model.TempData;
import com.debao.common.model.WechatTemplateVo;
import com.debao.common.utils.AliyunSmsUtil;
import com.debao.common.utils.SecurityUtils;
import com.debao.common.wechat.WechatUtils;
import com.debao.system.domain.GoodsOrder;
import com.debao.system.domain.StadiumTableOrder;
import com.debao.system.service.IGoodsOrderService;
import com.debao.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 商品订单
 */
@RestController
@RequestMapping("/api/goodsOrder")
@CrossOrigin
public class ApiGoodsOrder extends BaseController {

    @Autowired
    private IGoodsOrderService goodsOrderService;

    @Autowired
    private ISysUserService userService;

    /**
     * 获取用户商品订单
     *
     * @param goodsOrder
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(GoodsOrder goodsOrder) {
        goodsOrder.setUserId(SecurityUtils.getUserId());
        startPage();
        return getDataTable(goodsOrderService.selectGoodsOrderList(goodsOrder));
    }
    /**
     * 根据订单号获取详情
     */
    @GetMapping("/orderNoInfo/{orderNo}")
    public AjaxResult orderNoInfo(@PathVariable("orderNo") String orderNo) {
        return AjaxResult.success(goodsOrderService.selectGoodsOrderByOrderNo(orderNo));
    }

    /**
     * 获取订单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {
        return AjaxResult.success(goodsOrderService.selectGoodsOrderById(id));
    }

    /**
     * 订单是否支付成功
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/isPaySuccess/{orderNo}")
    public AjaxResult isPaySuccess(@PathVariable("orderNo") String orderNo) {
        return AjaxResult.success(goodsOrderService.isPaySuccess(orderNo));
    }

    /**
     * 取消支付
     */
    @PostMapping("/cancelPay/{orderNo}")
    public AjaxResult cancelPay(@PathVariable("orderNo") String orderNo) {
        GoodsOrder goodsOrder = goodsOrderService.selectGoodsOrderByOrderNo(orderNo);
        if (Objects.isNull(goodsOrder)) {
            return error("订单不存在");
        }
        if (!goodsOrder.getStatus().equals(OrderStatusEnum.None.name())) {
            return error("订单已处理");
        }
        goodsOrder.setStatus(OrderStatusEnum.Cancel.name());
        int row = goodsOrderService.updateGoodsOrder(goodsOrder);
        if (row == 1) {
            return AjaxResult.success("取消成功");
        }
        return error("取消失败");
    }
    /**
     *
     *用户催发货
     */
    @PostMapping("/remind/{orderNo}")
    public AjaxResult remind(@PathVariable("orderNo") String orderNo) {
        Long userId = SecurityUtils.getUserId();
        //异步方式实现短信推送 通知给用户
        Thread threadMsg = new Thread(() -> {
            synchronized (this) {
                //获取用户手机号码
                try {
                    SysUser user = userService.selectUserById(userId);
                    if (!user.getPhonenumber().isEmpty()) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("name", user.getNickName());
                        map.put("orderNo",orderNo);
                        AliyunSmsUtil.sendSms(user.getPhonenumber(), "SMS_471860008", JSONObject.toJSONString(map));
                    }
                } catch (Exception e) {
                    throw new RuntimeException("短信发送失败");
                }
            }
        });
        threadMsg.start();
        return AjaxResult.success("催发货成功");
    }
}
